package com.tgy.algorithm.base._单调栈;

import java.util.Stack;

/**
 * 给定一个只包含正整数的数组arr，arr中任何一个子数组sub一定都可以算出(sub累加和 )*(sub中的最小值)是什么。
 * 那么所有子数组中，这个值最大是多少?
 */
public class _数组中子数组最小值与子数组和的乘积 {

    public static int getSubArrMax(int[] nums) {

        int len = nums.length;
        if (nums == null || len == 0) {
            return 0;
        }

        Stack<Integer> stack = new Stack<>();

        int retVal = 0;
        int value;
        for (int i = 0; i < len; i++) {
            value = nums[i];
            while (!stack.isEmpty() && nums[stack.peek()] > value) {
                // 计算preIndex为最小值
                Integer preIndex = stack.pop();
                retVal = Math.max(retVal,sum(nums, stack.isEmpty() ? 0 : stack.peek(), i));
            }

            stack.push(i);
        }

        while (!stack.isEmpty()) {
            Integer preIndex = stack.pop();
        }

        return 0;
    }

    public static int sum(int[] nums,int start,int end) {
        if (start > end) {
            return 0;
        }

        int total = 0;
        for (int i = start; i <= end; i++) {
            total += nums[i];
        }

        return total;
    }

    public static void main(String[] args) {

        //
        int[] nums = {3,2,3,1,4,2};
        int subArrMax = getSubArrMax(nums);
        System.out.println(subArrMax);
    }
}
